package xworker.dataObject.data;

import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.dataObject.data.tree.TreeModel;
import xworker.dataObject.DataObject;
import xworker.dataObject.DataObjectList;
import xworker.dataObject.DataStore;

import java.util.HashSet;
import java.util.Set;

public class StaticDataManager {
    //private static final String TAG = StaticDataManager.class.getName();

    Thing thing;
    ActionContext actionContext = new ActionContext();

    public StaticDataManager(Thing thing){
        this.thing = thing;

        //创建各种数据
        for(Thing child : thing.getChilds()){
            child.doAction("create", actionContext);
        }
    }

    /**
     * 返回是否包含指定的变量。
     */
    public boolean contains(String key){
        return actionContext.containsKey(key);
    }

    /**
     * 返回所有变量名的Set。
     */
    public Set<String> ketySet(){
        Set<String> set = new HashSet<>();

        for(String key : actionContext.keySet()){
            Object obj = actionContext.get(key);
            if(obj instanceof DataObject || obj instanceof DataObjectList || obj instanceof DataStore || obj instanceof TreeModel){
                set.add(key);
            }
        }
        return set;
    }

    /**
     * 根据key返回相应的变量。
     */
    public <T> T get(String key){
        return actionContext.getObject(key);
    }

    public DataObject dataObject(String name){
        return actionContext.getObject(name);
    }

    public DataObjectList dataObjectList(String name){
        return actionContext.getObject(name);
    }

    public DataStore dataStore(String name){
        return actionContext.getObject(name);
    }

    public static void createChilds(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        for(Thing child : self.getChilds()){
            child.doAction("create", actionContext);
        }
    }

    public static StaticDataManager getStaticDataManager(Thing self){
        String key = "DataManager";
        StaticDataManager manager = self.getStaticData(key);
        if(manager == null){
            manager = new StaticDataManager(self);
            self.setStaticData(key, manager);
        }

        return manager;
    }

    //xworker.data.StaticDataManager/@actions/@reinit
    public static void reinit(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        String key = "DataManager";

        self.setStaticData(key, new StaticDataManager(self));
    }

    //xworker.data.StaticDataManager/@actions/@getDataManager
    public static StaticDataManager getStaticDataManager(ActionContext actionContext) {
        Thing self = actionContext.getObject("self");
        return getStaticDataManager(self);
    }

    public static void createDataObjects(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        for(Thing child : self.getChilds()){
            DataObject data = new DataObject(child);
            actionContext.g().put(child.getMetadata().getName(), data);
        }
    }

    public static void createDataObjectLists(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        for(Thing child : self.getChilds()){
            Thing dataObject = child.doAction("getDataObject", actionContext);
            actionContext.g().put(child.getMetadata().getName(), new DataObjectList(dataObject));
        }
    }

    public static void createDataStores(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        for(Thing child : self.getChilds()){
            child.doAction("create", actionContext);
        }
    }
}
